const webpackDevMiddleware = require('webpack-dev-middleware');

module.exports = (compiler, opts) => {
    const expressMiddleware = webpackDevMiddleware(compiler, opts);

    const koaMiddleware = async (ctx, next) => {
        await expressMiddleware(ctx.req, {
            end: content => {
                ctx.body = content;
            },
            setHeader: (name, value) => {
                ctx.set(name, value);
            },
            locals: ctx.state
        }, next);
    }

    koaMiddleware.getFilenameFromUrl = expressMiddleware.getFilenameFromUrl;
    koaMiddleware.waitUntilValid = expressMiddleware.waitUntilValid;
    koaMiddleware.invalidate = expressMiddleware.invalidate
    koaMiddleware.close = expressMiddleware.close
    koaMiddleware.fileSystem = expressMiddleware.fileSystem

    return koaMiddleware;
}